<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 员工服务 </el-breadcrumb-item>
        <el-breadcrumb-item>员工管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="danger"
                   icon="el-icon-delete"
                   class="handle-del mr10"
                   @click="delAllSelection">批量删除</el-button>
        <el-button type="primary"
                   icon="el-icon-delete"
                   class="handle-del mr10"
                   @click="addOne">新增</el-button>
        员工编号：<el-input v-model="query.id"
                  placeholder="员工编号"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>
        员工名称：<el-input v-model="query.employeName"
                  placeholder="员工名称"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>
        基础薪资：<el-input v-model="query.basicSalary"
                  placeholder="基础薪资"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>
        岗位工资：<el-input v-model="query.postSalary"
                  placeholder="岗位工资"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>
        奖金：<el-input v-model="query.allowance"
                  placeholder="奖金"
                  class="handle-input mr10"
                  style="width: 200px"></el-input>
        <!-- <el-select v-model="query.status"
                   placeholder="状态">
          <el-option v-for="item in statusList"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select> -->

        <el-select v-model="query.position"
                   placeholder="角色">
          <el-option v-for="item in positionList"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>

        </el-select>
        <el-select v-model="query.department"
                   placeholder="部门">
          <el-option v-for="item in departmentList"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value">
          </el-option>
        </el-select>

        <el-button type="primary"
                   icon="el-icon-search"
                   @click="handleSearch">搜索</el-button>
      </div>
      <el-table :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
                :row-key="getRowKeys">
        <el-table-column type="selection"
                         width="55"
                         align="center"
                         :reserve-selection="true"></el-table-column>
        <el-table-column prop="id"
                         label="员工编号"
                         width="55"
                         align="center"></el-table-column>
        <el-table-column prop="employeName"
                         label="员工名称"></el-table-column>
        <el-table-column prop="basicSalary"
                         label="基础薪资"> </el-table-column>
        <el-table-column prop="postSalary"
                         label="岗位工资"
                         align="center"></el-table-column>
        <el-table-column prop="allowance"
                         label="奖金"></el-table-column>
        <!-- <el-table-column prop="status"
                         label="状态"></el-table-column> -->
        <el-table-column prop="position"
                         label="角色"></el-table-column>
        <el-table-column prop="department"
                         label="部门"></el-table-column>
        <el-table-column label="操作"
                         width="200"
                         align="center">
          <template slot-scope="scope">
            <el-button type="text"
                       icon="el-icon-edit"
                       @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="text"
                       icon="el-icon-delete"
                       class="red"
                       @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination v-bind:child-msg="pageparm"
                    @callFather="callFather"></Pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑"
               :visible.sync="editVisible"
               width="30%">
      <el-form ref="form"
               :model="form"
               label-width="70px">
        <el-form-item label="员工名称">
          <el-input v-model="form.employeName">
            <template slot="append">@Saar</template>
          </el-input>
        </el-form-item>
        <el-form-item label="基础薪资">
          <el-input v-model="form.basicSalary"></el-input>
        </el-form-item>
        <el-form-item label="岗位工资">
          <el-input v-model="form.postSalary"></el-input>
        </el-form-item>
        <el-form-item label="奖金">
          <el-input v-model="form.allowance"></el-input>
        </el-form-item>
        <!-- <el-form-item label="状态">
          <el-select v-model="form.status"
                     placeholder="客户类型">
            <el-option v-for="item in statusList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="form.position"
                     placeholder="角色">
            <el-option v-for="item in positionList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门">
          <el-select v-model="form.department"
                     placeholder="部门">
            <el-option v-for="item in departmentList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../common/Pagination.vue';
export default {
    name: 'basetable',
    data() {
        return {
            statusList: [
                {
                    value: '国企',
                    label: '国企'
                },
                {
                    value: '私企',
                    label: '私企'
                }
            ],
            positionList: [
                {
                    value: '管理员',
                    label: '管理员'
                },
                {
                    value: '司机',
                    label: '司机'
                },
                {
                    value: '送货员',
                    label: '送货员'
                }
            ],
            departmentList: [
                {
                    value: '销售',
                    label: '销售'
                },
                {
                    value: '行政',
                    label: '行政'
                },
                {
                    value: '运输',
                    label: '运输'
                }
            ],
            isAdd: false,
            query: {
                id: '',
                employeName: '',
                basicSalary: '',
                postSalary: '',
                allowance: '',
                position: '',
                department: ''
            },
            formInline: {
                page: 1,
                limit: 5
            },
            // 分页参数
            pageparm: {
                currentPage: 1,
                pageSize: 5,
                total: 5
            },
            tableData: [],
            current: '',
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    // 注册组件
    components: {
        Pagination
    },
    created() {
        this.getData(this.formInline);
    },
    methods: {
        // 获取 培训项目数据
        getData(parameter) {
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.$axios
                .post('/api/employe/findAll/' + this.pageparm.currentPage + '/' + this.pageparm.pageSize, this.query)
                .then((res) => {
                 // this.query.employeName=this.query.employeName.replace("@Saar","");
                  let employeList=res.data.data.records;
                    for (let i = 0; i < employeList.length; i++) {
                        employeList[i].employeName=employeList[i].employeName.replace("@Saar","").trim();
                    }
                    this.tableData = employeList;
                    // console.log(this.tableData);
                    this.pageparm.total = res.data.data.total;
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        // 分页插件事件
        callFather(parm) {
            //子传父 当前页和每页大小
            this.formInline.page = parm.currentPage;
            this.formInline.limit = parm.pageSize;
            this.getData(this.formInline);
        },
        getRowKeys(row) {
            return row.id;
        },
        // 触发搜索按钮
        handleSearch() {
            this.query.employeName=this.query.employeName+"@Saar"
            this.$set(this.pageparm, 'currentPage', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    this.$axios
                        .post('/api/employe/delete', { id: row.id })
                        .then((res) => {
                            console.log(res);
                            if (res.data.code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: '数据已删除!'
                                });
                                //重新获取当前页面数据重新渲染列表！
                                this.getData(this.pageparm);
                            } else {
                                this.$message({
                                    type: 'error',
                                    message: res.data.message
                                });
                            }
                        })
                        .catch((err) => {
                            //this.$message.error('数据删除失败，请稍后再试！')
                            console.log(err);
                        });
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 多选操作
        handleSelectionChange(val) {
            //console.log(val);
            this.multipleSelection = val;
        },
        addOne() {
            this.isAdd = true;
            this.form = {};
            this.editVisible = true;
        },
        delAllSelection() {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    //console.log(this.multipleSelection);
                    const length = this.multipleSelection.length;
                    let str = '';
                    this.delList = this.delList.concat(this.multipleSelection);
                    for (let i = 0; i < length; i++) {
                        str += this.multipleSelection[i].trainingname + ' ';
                        console.log(this.multipleSelection[i].id);
                        this.$axios
                            .post('/api/employe/delete', { id: this.multipleSelection[i].id })
                            .then((res) => {
                                console.log(res);
                                if (res.data.code == 200) {
                                    this.$message({
                                        type: 'success',
                                        message: '数据已删除!'
                                    });
                                    //重新获取当前页面数据重新渲染列表！
                                    this.getData(this.pageparm);
                                } else {
                                    this.$message({
                                        type: 'info',
                                        message: res.data.message
                                    });
                                }
                            })
                            .catch((err) => {
                                //this.$message.error('数据删除失败，请稍后再试！')
                                console.log(err);
                            });
                        //console.log(str);
                    }
                    this.$message.error(`删除了${str}`);
                    this.multipleSelection = [];
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
        },
        // 编辑操作
        handleEdit(index, row) {
            this.isAdd = false;
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
          this.form.employeName=this.form.employeName+"@Saar"
            this.editVisible = false;
            if (this.isAdd) {
                this.$axios
                    .put('/api/employe/addOne', this.form)
                    .then((res) => {
                        if (res.data.code == 200) {
                            this.getData(this.pageparm);
                            this.$message({
                                type: 'success',
                                message: '新增员工成功！'
                            });
                            this.editFormVisible = false;
                        } else {
                            this.$message({
                                type: 'info',
                                message: res.data.message
                            });
                            this.editFormVisible = false;
                        }
                    })
                    .catch();
                return;
            }
            // this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            //this.$set(this.tableData, this.idx, this.form);
            this.$axios
                .post('/api/employe/updateOne/', this.form)
                .then((res) => {
                    if (res.data.code == 200) {
                        this.getData(this.pageparm);
                        this.$message({
                            type: 'success',
                            message: '数据修改成功！'
                        });
                        this.editFormVisible = false;
                    } else {
                        this.$message({
                            type: 'info',
                            message: res.data.message
                        });
                        this.editFormVisible = false;
                    }
                })
                .catch();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
